package com.dxyxx.framework;

import java.util.Date;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.pierry.simpletoast.SimpleToast;

import com.dxyxx.R;
import com.dxyxx.dialog.ViewTouchListener;
import com.dxyxx.pojo.Group;
import com.dxyxx.pojo.Order;
import com.dxyxx.pojo.OrderDetail;

/**
 * 专门为了 开团吧券跟正常订单
 * 
 * @author Administrator
 * 
 */
public class PayActivity extends BasePayActivity {
	private Order order;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay);
		// 返回首页 动态效果。
		TextView backToHome = (TextView) findViewById(R.id.backToHome);
		backToHome.setOnTouchListener(new ViewTouchListener(PayActivity.this, backToHome));
		backToHome.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(PayActivity.this, FragmentMainActivity.class));
			}
		});
		ViewGroup rootViewGroup = (ViewGroup) findViewById(R.id.content);
		Bundle bundle = this.getIntent().getExtras();
		order = (Order) bundle.getSerializable("order");

		initViews();
		if (order != null && order.getOrderDetails() != null) {//GroupOrder 没有detail
			for (OrderDetail od : order.getOrderDetails()) {
				View view = (ViewGroup) getLayoutInflater().inflate(R.layout.item_orderconfirm, null, false);
				((TextView) view.findViewById(R.id.price)).setText(od.getPrice() + "");
				((TextView) view.findViewById(R.id.amount)).setText("*" + od.getAmount() + "=  ");
				((TextView) view.findViewById(R.id.all)).setText(df.format(od.getPrice() * od.getAmount()));
				((TextView) view.findViewById(R.id.description)).setText(od.getProduct().getDescription());
				((TextView) view.findViewById(R.id.specification)).setText(od.getProduct().getSpecification());
				imageLoader.displayImage(baseImageURL + od.getProduct().getThumbId() + "?p=0", (ImageView) view.findViewById(R.id.thumb), squareOptions, animateFirstListener);

				rootViewGroup.addView(view);
			}
		}
		//团购订单
		if (order != null & order.getGroup() != null) {
			Group group = order.getGroup();
			View view = (ViewGroup) getLayoutInflater().inflate(R.layout.item_orderconfirm, null, false);
			((TextView) view.findViewById(R.id.price)).setText(group.getPersonPrice() + "");
			((TextView) view.findViewById(R.id.amount)).setText("*" + 1 + "=  ");
			((TextView) view.findViewById(R.id.all)).setText(df.format(group.getPersonPrice()));
			((TextView) view.findViewById(R.id.description)).setText(group.getProduct().getDescription());
			((TextView) view.findViewById(R.id.specification)).setText(group.getProduct().getSpecification());
			imageLoader.displayImage(baseImageURL + group.getProduct().getThumbId() + "?p=0", (ImageView) view.findViewById(R.id.thumb), squareOptions, animateFirstListener);
			rootViewGroup.addView(view);
		}
	}

	public void initViews() {
		((ImageView) findViewById(R.id.backImageButton)).setVisibility(View.GONE);
		((ImageView) findViewById(R.id.nextImageButton)).setVisibility(View.INVISIBLE);
		TextView title = (TextView) findViewById(R.id.title);
		TextView orderMoney = (TextView) findViewById(R.id.orderMoney);
		TextView orderDeliveryType = (TextView) findViewById(R.id.orderDeliveryType);
		TextView orderId = (TextView) findViewById(R.id.orderId);
		//TextView et_payType = (TextView) findViewById(R.id.et_payType);
		ImageView et_payType = (ImageView) findViewById(R.id.payIcon);

		orderMoney.setText("￥" + df.format(order.getMoney()));
		 
		switch (order.getBufferInterval()) {
		case 0:
			orderDeliveryType.setText("当天");
			break;
		case 1:
			orderDeliveryType.setText("下单后第二天");
			break;
		case 2:
			orderDeliveryType.setText("下单后第三天");
			break;

		default:
			orderDeliveryType.setText("当天");
			break;
		}

		orderId.setText(order.getUuid());
		if (order.getPayType().equals("alipay")) {
			et_payType.setImageResource(R.drawable.alipay_icon);
		} else {
			et_payType.setImageResource(R.drawable.wechat);
		}
		title.setText("订单提交成功");
		title.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);

		findViewById(R.id.payButton).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!jisuan(order.getPostTime(), new Date())) {
					SimpleToast.error(PayActivity.this, "订单已经超过24小时，无效了");
					return;
				}
				if (order.getPayType().equals("tenpay")) {
					progressDialog = ProgressDialog.show(PayActivity.this, null, null);
					progressDialog.setContentView(R.layout.dialog_circle);
					tenPay(PayActivity.this, progressDialog, order.getUuid());
				} else if (order.getPayType().equals("alipay")) {
					alipay(order.getUuid(),order.getMoney());
					SimpleToast.info(getApplicationContext(), "支付宝");
				} else {
					//...
				}
			}
		});

	}
}
